package PlayerManager;

import java.util.LinkedList;
import java.util.Random;

import GameFiles.GameManager;
import GameFiles.StateBase;
import Gui.GameFrame;

public class PlayerManager {
	private static final int NUM_OF_FACES = 6;

	private static PlayerManager ref = null;
	private int m_currentPlayer = 0;
	private int ID = 0;
	private LinkedList<Player> PlayerList;
	private LinkedList<Chance> m_lottoPossibilities;

	public void initLotto() {
		m_lottoPossibilities = new LinkedList<Chance>();
		for (int i = 1; i <= NUM_OF_FACES; i++) {
			for (int j = 1; j <= NUM_OF_FACES; j++) {
				Chance possilbity = new Chance(i, j);
				m_lottoPossibilities.add(possilbity);

			}

		}
	}

	public Chance removePossibilityFromPool(){
		Random rand = new Random();
		return m_lottoPossibilities.remove(rand.nextInt(m_lottoPossibilities.size()));
	}
	@SuppressWarnings("unchecked")
	private PlayerManager() {
		PlayerList = new LinkedList();
		//initLotto();
	}

	public static PlayerManager getInstance() {
		if (ref == null) {
			ref = new PlayerManager();
		}

		return ref;
	}

	public LinkedList<Player> getPlayerList() {
		return PlayerList;
	}

	/**
	 * Changes the CurrentPlayers State to newState
	 */
	public void ChangeCurrentPlayerState(StateBase newState) {
		PlayerList.get(m_currentPlayer).getCurrentState().Exit();
		PlayerList.get(m_currentPlayer).setPreviousState(
				PlayerList.get(m_currentPlayer).getCurrentState());
		PlayerList.get(m_currentPlayer).setCurrentState(newState);
		PlayerList.get(m_currentPlayer).getCurrentState().Enter();
		PlayerList.get(m_currentPlayer).getCurrentState().Draw(
				GameFrame.getInstance());
	}

	/**
	 * Switches PreviousState and CurrentState for CurrentPlayer
	 */
	public void ChangePreviousState() {
		PlayerList.get(m_currentPlayer).getCurrentState().Exit();
		StateBase TempState = PlayerList.get(m_currentPlayer).getCurrentState();
		PlayerList.get(m_currentPlayer).setCurrentState(
				PlayerList.get(m_currentPlayer).getPreviousState());
		PlayerList.get(m_currentPlayer).setPreviousState(TempState);
		PlayerList.get(m_currentPlayer).getCurrentState().Enter();
	}

	// generate player list from currentPlayer number
	public void PopulatePlayerList() {
		ID = 0; // reset ID to 0; 
		for (int x = 0; x < GameManager.getInstance().getNumberOfPlayers(); x++) {
			Player newPlayer = new Player();
			newPlayer.setCash(3500);
			newPlayer.setDay(0);
			newPlayer.setID(ID++);
			newPlayer.setName("TempPlayer");

			PlayerList.add(newPlayer);
		}
	}

	public void SetCurrentPlayerTurn(int playerID) {
		m_currentPlayer = playerID;
	}

	public void ChangePlayerTurn() {
		m_currentPlayer++;
		if (m_currentPlayer == GameManager.getInstance().getNumberOfPlayers()) {
			m_currentPlayer = 0;
		}
	}

	public int GetCurrentPlayerTurn() {
		return m_currentPlayer;
	}

	// gameloops processes for player state
	// TODO - I removed 'static' from Draw and Update.
	public void Draw(GameFrame gf) {
		PlayerList.get(m_currentPlayer).Draw(gf);
	}

	public void Update() {
		PlayerList.get(m_currentPlayer).Update();
	}
}
